import request from '../utils/request'

// 学生登录：学号 + 身份证后六位
export function studentLogin(data) {
  // data: { stuNo: string|number, idTail6: string }
  // 后端期望 @RequestParam stuNo, password
  const form = new URLSearchParams()
  form.append('stuNo', data.stuNo)
  form.append('password', data.idTail6)
  return request({
    url: '/student-user/login',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: form
  })
}

// 教师登录：工号 + 身份证后六位
export function teacherLogin(data) {
  // data: { teacherNo: string, idTail6: string }
  // 后端期望 @RequestParam teNo, password
  const form = new URLSearchParams()
  form.append('teNo', data.teacherNo)
  form.append('password', data.idTail6)
  return request({
    url: '/teacher-user/login',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: form
  })
}

// 管理员登录：管理员编号 + 密码
export function adminLogin(data) {
  // data: { adminNo: string, adminPassword: string }
  return request({
    url: '/admin/login',
    method: 'post',
    data
  })
}

// 退出登录（通用接口）
export function logout(data) {
  // data: { userId: string, role: string }
  // 构建查询参数
  const params = []
  if (data?.userId) {
    params.push(`userId=${encodeURIComponent(data.userId)}`)
  }
  if (data?.role) {
    params.push(`role=${encodeURIComponent(data.role)}`)
  }
  
  const queryString = params.length > 0 ? `?${params.join('&')}` : ''
  
  // 使用GET请求，更简单可靠
  return request({
    url: `/auth/logout${queryString}`,
    method: 'get'
  })
}

